package org.jeecg.modules.basesource.handler;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import com.xxl.job.core.log.XxlJobLogger;
import jodd.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.fk.entity.DjOrg;
import org.jeecg.modules.fk.service.IDjOrgService;
import org.jeecg.modules.jinke.entity.GdScysCorpInfo;
import org.jeecg.modules.jinke.service.IGdScysCorpInfoService;
import org.jeecg.modules.system.entity.SysDepart;
//import org.jeecg.modules.system.service.IJhZzjgService;
import org.jeecg.modules.system.service.ISysDepartService;
import org.jeecg.modules.system.service.ISysDictService;
import org.jeecg.modules.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.List;

@Slf4j
@Component
public class DjOrgSyncHandler {
    @Autowired
    private IDjOrgService djOrgService;
    @Autowired
    private IGdScysCorpInfoService gdScysCorpInfoService;
    @Autowired
    private ISysDepartService sysDepartService;
    @Autowired
    private ISysDictService sysDictService;
    //    @Autowired
//    private IJhZzjgService jhZzjgService;
    @Autowired
    private ISysUserService sysUserService;

    //默认是其他地区
    public final static String DEFAULT_DEPART_CODE = "A04A09";

    @XxlJob(value = "djOrgSyncJob")
    public ReturnT<String> djOrgSyncJob(String s) {
        List<DjOrg> list = djOrgService.list();
        //如果没有关联部门，则默认到广东省分行，先查询默认部门
        LambdaQueryWrapper<SysDepart> dpEw = Wrappers.lambdaQuery(SysDepart.class);
        dpEw.eq(SysDepart::getOrgCode, DEFAULT_DEPART_CODE);
        SysDepart defaultDepart = sysDepartService.getOne(dpEw);
        for (DjOrg djOrg : list) {
            try {
                String customerCode = djOrg.getCustomerCode();
                if (StringUtils.hasLength(customerCode)
                        //todo 如果匹配不到系统中的部门，则有可能没有城市编号，这种情况是否需要重新调度，若需要则执行多次保存。
                        //&& StringUtils.hasLength(djOrg.getCityCode())
                        && StringUtils.hasLength(djOrg.getIndustry())
                        && StringUtils.hasLength(djOrg.getSaleNetCode())
                        && StringUtils.hasLength(djOrg.getSaleNetName())
                        && StringUtils.hasLength(djOrg.getOrgType())) {
                    continue;
                }
                List<GdScysCorpInfo> infoList = gdScysCorpInfoService.getFirstByBusinessCode(djOrg.getShxydm());
                if (infoList.size() <= 0) {
                    //找不到行内的企业信息，则更新为默认部门
                    if (!StringUtils.hasLength(djOrg.getSaleNetCode())) {
                        djOrg.setSaleNetCode(defaultDepart.getOrgCode());
                        djOrg.setSaleNetName(defaultDepart.getDepartName());
                        djOrgService.updateById(djOrg);
                    }
                    continue;
                }
                //行内企业表有关联
                GdScysCorpInfo gdScysCorpInfo = infoList.get(0);
                djOrg.setCustomerCode(gdScysCorpInfo.getCusCode());
                String indCategoryCode = gdScysCorpInfo.getIndCategoryCode();
                djOrg.setManageUserId(gdScysCorpInfo.getUserNo());
                djOrg.setManageUserName(gdScysCorpInfo.getUserName());
                String ecoNameSecond = gdScysCorpInfo.getEcoNameSecond();
                djOrg.setIndustry(indCategoryCode);
                if (StringUtils.hasLength(ecoNameSecond)) {
                    djOrg.setOrgType(ecoNameSecond.contains("国有") ? "01" : "02");
                } else {
                    djOrg.setOrgType("02");
                }
                // String industry = sysDictService.getItemValueByItemText(indCategoryCode, "industry_type");
                //通过行内的机构编码来查询系统的部门信息
                String code = gdScysCorpInfo.getOrgCode();
                SysDepart sysDepartCode  = null;
                if (StringUtil.isNotBlank(code)){
                    sysDepartCode = sysDepartService.getSysDepartByJianhangCode(code);
                }
                //匹配不到系统的部门信息,则放到默认的部门
                if (sysDepartCode == null) {
                    if (!StringUtils.hasLength(djOrg.getSaleNetCode())) {
                        djOrg.setSaleNetCode(defaultDepart.getOrgCode());
                        djOrg.setSaleNetName(defaultDepart.getDepartName());
                    }
                } else {
                    djOrg.setSaleNetCode(sysDepartCode.getOrgCode());
                    djOrg.setSaleNetName(sysDepartCode.getDepartName());
                    djOrg.setCityCode(sysDepartCode.getCityCode());
                }
                djOrgService.updateById(djOrg);
            } catch (Exception e) {
                XxlJobLogger.log("企业补偿失败" + e.getMessage());
                log.error(e.getMessage());
            }
        }
        return ReturnT.SUCCESS;
    }
}
